package com.wude.guild.adapter;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.mc.developmentkit.utils.MCUtils;
import com.mc.developmentkit.utils.ToastUtil;
import com.wude.bean.GiftInfo;
import com.wude.guild.R;
import com.wude.guild.fragment.MyGiftFragment;
import com.wude.guild.views.CustomSwipeListView;
import com.wude.guild.views.SwipeItemView;
import com.wude.http.HttpCom;
import com.wude.tools.Utils;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 我的礼包Adapter
 * Created by Administrator on 2017/3/20.
 */
@SuppressLint("HandlerLeak")
public class MyGiftListAdapter extends BaseAdapter {

    private final MyGiftFragment con;
    public List<GiftInfo> list = new ArrayList<GiftInfo>();
    private SwipeItemView mLastSlideViewWithStatusOn;
    private int pos1;

    public MyGiftListAdapter(MyGiftFragment activity) {
        this.con = activity;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        SwipeItemView slideView = (SwipeItemView) convertView;
        if (convertView == null) {
            View itemView = LayoutInflater.from(x.app()).inflate(R.layout.holder_mygift, null);
            slideView = new SwipeItemView(x.app());
            slideView.setContentView(itemView);
            viewHolder = new ViewHolder(slideView);
            slideView.setOnSlideListener(new SwipeItemView.OnSlideListener() {

                @Override
                public void onSlide(View view, int status) {
                    if (mLastSlideViewWithStatusOn != null && mLastSlideViewWithStatusOn != view) {
                        mLastSlideViewWithStatusOn.shrink();
                    }
                    if (status == SLIDE_STATUS_ON) {
                        mLastSlideViewWithStatusOn = (SwipeItemView) view;
                    }
                }
            });
            slideView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) slideView.getTag();
        }
        final GiftInfo item = list.get(position);
        if (CustomSwipeListView.mFocusedItemView != null) {
            CustomSwipeListView.mFocusedItemView.shrink();
        }
        MCUtils.fillImage(viewHolder.home_game_icon, item.icon);
        String jieQu = TextUtils.isEmpty(item.game_name) ? "" : Utils.getJieQu(item.game_name);
        viewHolder.gift_name.setText("《" + jieQu + "》" + item.giftbag_name);
        String dataYMD = TextUtils.isEmpty(item.start_time) || item.start_time.equals("null") ? "" :
                MCUtils.getDataYMD(item.start_time);
        if (item.end_time.equals("0")) {
            viewHolder.gitf_time.setText("有效期：" + dataYMD + " — " + "永久有效");
        } else {
            String dataYMD1 = TextUtils.isEmpty(item.start_time) || item.start_time.equals("null") ? "" :
                    MCUtils.getDataYMD(item.end_time);
            viewHolder.gitf_time.setText("有效期：" + dataYMD + " — " + dataYMD1);
        }
        viewHolder.jihuoma.setText(item.jihuoma);
        viewHolder.fuzhi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.fuzhi:
                        Utils.copy(item.jihuoma);
                        break;
                }
            }
        });
        viewHolder.deleteHolder.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                pos1 = position;
                removeCheckedItem(position, Utils.getIMEI(x.app()), item);
            }
        });
        return slideView;
    }


    private void removeCheckedItem(int pos, String imei, GiftInfo item) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("gift_id", item.gift_id + "");
        map.put("token", Utils.getLoginUser().token);
        Log.e("adapter的删除参数", item.gift_id + "" + "++token++" + imei);
        HttpCom.POST(mhandler, HttpCom.getDelGiftURL(), map, false);
    }


    public void setList(List<GiftInfo> b) {
        this.list = b;
        notifyDataSetChanged();
    }

    public List<GiftInfo> getList() {
        return list;
    }

    class ViewHolder {

        public ViewHolder(View convertView) {
            home_game_icon = convertView.findViewById(R.id.home_game_icon);
            gift_name = convertView.findViewById(R.id.gift_name);
            gitf_time = convertView.findViewById(R.id.gitf_time);
            jihuoma = convertView.findViewById(R.id.jihuoma);
            fuzhi = convertView.findViewById(R.id.fuzhi);
            deleteHolder = convertView.findViewById(R.id.holder);
        }

        public ImageView home_game_icon;
        public TextView gift_name;
        public TextView gitf_time;
        public TextView fuzhi;
        public TextView jihuoma;
        public ViewGroup deleteHolder;
    }


    Handler mhandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    Log.e("删除已领取礼包结果", msg.obj.toString());
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        int status = jsonObject.getInt("status");
                        String msg1 = jsonObject.getString("return_code");
                        if (status == 1) {
                            list.remove(pos1);
                            con.genxin(list);
                            notifyDataSetChanged();
                        } else {
                            ToastUtil.showToast(msg1);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        Log.e("解析删除礼包异常", e.toString());
                    }
                    break;
                case 2:
                    ToastUtil.showToast("网络异常");
                    break;
            }
        }
    };
}
